<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{URL::asset('layui/css/layui.css')}}"  media="all">
</head>
<body>
<fieldset class="layui-elem-field" style="margin-top: 20px;">
    <legend>搜索查询</legend>
    <form class="layui-form" action="">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">ID</label>
                <div class="layui-input-inline">
                    <input type="text" name="id" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="goods_name" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
                </div>
            </div>
        </div>
    </form>
</fieldset>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    {{--<a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="sku">添加规格</a>--}}
    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="ping">评价</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="goods_data">统计</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="{{URL::asset('layui/layui.js')}}" charset="utf-8"></script>
<script src="{{URL::asset('js/jquery-3.1.1.min.js')}}"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','upload','form'], function(){
        var table = layui.table,
            form = layui.form,
            upload=layui.upload;

        table.render({
            elem: '#test'
            ,url:'/admin/goods/list/api'
            ,toolbar: '#toolbarDemo'
            ,title: '商品表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'class_name', title:'类型名称', width:100}
                ,{field:'goods_name', title:'商品名称',width:250}
                ,{field:'price', title:'价格',width:100}
                ,{field:'is_free_shopping', title:'邮费',width:100}
                ,{field:'sku', title:'规格',width:300,templet:function (d) {
                        if (d.sku.length>0){
                            var jd = '';
                            $.each(d.sku,function (i,v) {
                                jd += '<span class="layui-badge layui-bg-blue" style="margin-right: 2px;">'+v.sku_name+'</span>';
                            });
                            return jd;
                        } else {
                            return '未上传';
                        }
                    }}
                ,{field:'color', title:'颜色',width:300,templet:function (d) {
                        if (d.color.length>0){
                            var jd = '';
                            $.each(d.color,function (i,v) {
                                jd += '<span class="layui-badge layui-bg-blue" style="margin-right: 2px;">'+v.color_name+'</span>';
                            });
                            return jd;
                        } else {
                            return '未上传';
                        }
                    }}
                ,{field:'created_at', title:'加入时间',width:200}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo',width:200}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    layer.open({
                        type:2,
                        title:"新增商品",
                        area:["90%","80%"],
                        content:"/admin/goods/add"
                    });
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('该操作降永久删除此数据,是否确认?', function(index){
                    $.post('/admin/goods/list/delete',{"id":data.id},function (d) {
                        if (d.code==200){
                            layer.msg("删除成功");
                            setTimeout(function () {
                                obj.del();
                                layer.close(index);
                            },500)
                        } else {
                            layer.msg("删除失败");
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type:2,
                    title:"编辑商品信息",
                    area:["90%","80%"],
                    content:'/admin/goods/edit?id='+data.id
                });
            } else if(obj.event === 'sku'){
                layer.open({
                    title:"新增商品规格",
                    area:["80%","30%"],
                    content:'<fieldset class="layui-elem-field site-demo-button" style="margin-top: 20px;">\n' +
                        '    <legend>商品规格</legend>\n' +
                        '    <div style="margin-top: 10px;" class="sku_div">\n' +
                        '        <div class="layui-form-item">\n' +
                        '            <label class="layui-form-label">规格</label>\n' +
                        '            <div class="layui-input-inline">\n' +
                        '                <input type="text" id="sku_name" autocomplete="off" placeholder="规格名称" class="layui-input">\n' +
                        '            </div>\n' +
                        '            <div class="layui-input-inline">\n' +
                        '                <input type="text" id="sku_price" autocomplete="off" placeholder="价格" class="layui-input">\n' +
                        '            </div>\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '</fieldset>',
                    btn:["保存","取消"],
                    yes:function () {
                        var sku_name = $('#sku_name').val();
                        var sku_price = $('#sku_price').val();
                        var url = "/admin/goods/sku/add";
                        var where = {"goods_id":data.id,"sku_price":sku_price,"sku_name":sku_name};
                        $.post(url,where,function (d) {
                            if (d.code==200){
                                layer.msg("商品规格添加成功");
                                setTimeout(function () {
                                    window.location.reload();
                                },500);
                            }else{
                                layer.msg("商品规格添加失败");
                            }
                        });
                    }
                });
            } else if(obj.event === 'color'){
                layer.open({
                    title:"新增商品颜色",
                    area:["80%","30%"],
                    content:'<fieldset class="layui-elem-field site-demo-button" style="margin-top: 20px;">\n' +
                        '    <legend>商品颜色</legend>\n' +
                        '    <div style="margin-top: 10px;" class="color_div">\n' +
                        '        <div class="layui-form-item">\n' +
                        '            <label class="layui-form-label">颜色</label>\n' +
                        '            <div class="layui-input-inline">\n' +
                        '                <input type="text" id="color_name" autocomplete="off" placeholder="规格名称" class="layui-input">\n' +
                        '            </div>\n' +
                        '            <div class="layui-input-inline">\n' +
                        '                <input type="file" name="files" autocomplete="off"  class="layui-input">\n' +
                        '            </div>\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '</fieldset>',
                    btn:["保存","取消"],
                    yes:function () {
                        var color_name = $('#color_name').val();
                        var image = $("input[name='files']").get(0).files[0];

                        var formdata = new FormData();
                        formdata.append("goods_id",data.id);
                        formdata.append("color_name",color_name);
                        formdata.append("files",image);

                        var url = "/admin/goods/color/add";

                        $.ajax({
                            url: url,
                            type: "POST",
                            dataType: "json",
                            data: formdata,
                            async: false,
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function (d) {
                                if (d.code==200){
                                    layer.msg("商品颜色添加成功");
                                    setTimeout(function () {
                                        window.location.reload();
                                    },500);
                                }else{
                                    layer.msg("商品颜色添加失败");
                                }
                            },
                        });
                    }
                });
            } else if(obj.event === 'goods_data'){
                var l = parent === self ? layui : top.layui;
                l.index.openTabsPage('/admin/goods/data/html?id='+data.id, '商品统计')
            } else if(obj.event === 'ping'){
                var l = parent === self ? layui : top.layui;
                l.index.openTabsPage('/admin/ping/html?id='+data.id, '商品评价')
            }
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            var param = data.field;
            //执行重载
            table.reload('test', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    id: param.id,
                    goods_name:param.goods_name,
                }
            });
            return false;
        });

    });
</script>

</body>
</html>